// 返回
$(document).on('tap', '.my_order_back', function() {
	mui.back();
})
mui.init();
var mask = mui.createMask(); //遮罩层
var modal = document.getElementById('myModal'); // 获取弹窗  
//页面进入执行方法
var id,silver_order_no,pay_password,time,numberDetail,balance,status,silberId; 
window.onload = function() {
	mui.plusReady(function() {
		self = plus.webview.currentWebview();
		silberId = self.silberId;
		console.log(silberId)
		Detail()
	});
}
function Detail(){
	mui.ajax(user_url + '/api/silver/recordInfo',{
		data:{
			id:silberId
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success:function(data){
			console.log(JSON.stringify(data))
			$('#orderId').html(data.resp.silver_order_no)
			silver_order_no = data.resp.silver_order_no;
			$('#moneyId').html("-"+data.resp.sum_price)
			$('#timeId').html(data.resp.createtime)
		}
	})
}
document.getElementById("sub_refund").addEventListener("tap", function() {
	 mui.ajax(user_url + '/api/Silver/requestRefund',{
	 	data:{
			silver_order_no: silver_order_no
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success:function(data){
			console.log(JSON.stringify(data))
			var btnArray = ['是', '否'];
				mui.confirm('您当前该购买记录已转换'+data.resp.ecptoma+'银宝,仅可退'+data.resp.balance+'元,同时清除'+data.resp.sum_money+'银宝。是否申请退款？','退款确认', btnArray, function(e) {
			        if (e.index == 1) {
			           console.log('否');
			        }
			        else {
			        	console.log('是');
			        	payPanel.showPanel(function(){
			        		payPanel.closePanel();//支付窗口
			        		judge_payment_password()
			        	})
			        	
			        }
			   })
		}
	 })
});

//确认密码
function judge_payment_password(){
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	//判断支付密码是否正确
	console.log(pwds)
	var cc = {
			"password":pwds, //支付密码
			"silver_order_no":silver_order_no
		}
	console.log(JSON.stringify(cc))
	mui.ajax(goods_url + '/api/Silver/affirmRefund', { //4.确认支付（判断支付密码是否正确）接口
		data:{
			"pay_password":pwds, //支付密码
			"silver_order_no":silver_order_no
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			console.log('支付提交');
			console.log(JSON.stringify(data))
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			if(data.code == 10001){		
				mui.toast(data.msg);
			}else if (data.code == 789){
				mui.toast(data.msg);
			}else if (data.code == 10003 ){
				mui.toast(data.msg);
			}else if (data.code == 10004 ){
				mui.toast(data.msg);
			}else if (data.code == 10000 ){
				payPanel.closePanel();//支付窗口
				mui.toast(data.msg);
				silver_order_no = data.resp.silver_order_no;
				time = data.resp.time;
				numberDetail = data.resp.number;
				balance =  data.resp.balance;
				status = data.resp.status
				mui.openWindow({ //跳转到我的订单
					url: '../my/yinDetail_success.html',
					extras: {
						silberId: self.silberId,
						silver_order_no:silver_order_no,
						time: time,
						numberDetail :numberDetail,
						balance:balance,
						status : status
					},
					createNew: true
				});
				
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			console.log('确认密码接口失败');
//			var error_msg = JSON.parse(xhr.responseText)
//			mui.alert(error_msg.msg)
		}
	});
}


/*==================================================
输入支付密码  开始
 ==================================================*/
(function() {
	window.payPanel = {
		/**
		 * 显示输入面板，指定输入完成后的回调事件和点击取消的回调时间
		 * @param {Object} _inputOverFn
		 * @param {Object} _cancelFn
		 */
		showPanel: function(_inputOverFn, _cancelFn) {
			//输入完成后的回调
			panelObj.inputOverFn = _inputOverFn;
			
			//取消后的回调
			panelObj.cancelFn = _cancelFn;

			document.getElementsByClassName("pays")[0].style.display = "block";
			this.isShow = true; 
		},
		/**
		 * 关闭输入面板
		 */
		closePanel: function() {
			document.getElementsByClassName("pays")[0].style.display = "block";
			panelObj.clearAll();
			this.isShow = false;
		},
		isShow:false
	}
	//输入面板的对象及其所有方法
	var panelObj = {
		//输入面板内容
		template: "<div class='payPanel'><div class='pass'><i class='mui-icon mui-icon-closeempty close'></i><h4>请输入支付密码</h4><p id='pay_price'></p><ul><li></li><li></li><li></li><li></li><li></li><li></li></ul></div><div class='inputPanel'><div><ul><li><a data-value='1'>1</a></li><li><a data-value='2'>2</a></li><li><a data-value='3'>3</a></li><li><a data-value='4'>4</a></li><li><a data-value='5'>5</a></li><li><a data-value='6'>6</a></li><li><a data-value='7'>7</a></li><li><a data-value='8'>8</a></li><li><a data-value='9'>9</a></li><li class='cancel'><a data-value='cancel'>取消</a></li><li><a data-value='0'>0</a></li><li><a data-value='delete'>删除</a></li></ul></div></div></div>",
		//初始化面板，将面板添加到页面
		init: function() {
			var div = document.createElement("div");
			div.className = "pays";
			div.innerHTML = this.template;
			document.body.appendChild(div);
			this.addEvent();
		},
		//输入的密码值
		inputVal: "",
		//添加键盘点击事件
		addEvent: function() {
			var inputLi = document.getElementsByClassName("inputPanel")[0].getElementsByTagName("a");
			for(var i = 0, len = inputLi.length; i < len; i++) {
				var ele = inputLi[i];
				ele.addEventListener("touchstart", function(e) {
					e.preventDefault();
					var value = this.getAttribute("data-value");
					if(value == "cancel") {
						//取消支付
						panelObj.clearAll();
						payPanel.closePanel();
						panelObj.cancelFn!=undefined?panelObj.cancelFn():"";

					} else if(value == "delete") {
						//删除
						var len = panelObj.inputVal.length;
						panelObj.inputVal = panelObj.inputVal.substr(0, len - 1);
						panelObj.clearInputContent(len - 1);

					} else {
						if(panelObj.inputVal.length >= 6) return;
						panelObj.inputVal += value;
						//设置输入框内容
						panelObj.setInputContent();
						if(panelObj.inputVal.length == 6) {
							//延迟2毫秒,保证用户可以看到输入了第六位
							setTimeout(function(){
								pwds = panelObj.inputVal;
								panelObj.inputOverFn!=undefined?panelObj.inputOverFn(panelObj.inputVal):"";
								panelObj.clearAll();
							},200)
							
						}

					}

				})
			}
		},
		//给6个输入款填充内容
		setInputContent: function() {
			var index = panelObj.inputVal.length - 1;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[index].className = " i-circle";
		},
		//给指定的输入款取消内容
		clearInputContent: function(_index) {
			if(_index<0)return;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[_index].className = "";
		},
		//清除所有输入款内容
		clearAll: function() {
			this.inputVal = "";
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			for(var i = 0, len = passLi.length; i < len; i++) {
				passLi[i].className = "";
			}
		}, 
	}
	panelObj.init();
	// 点击 (x), 关闭弹窗
	$(document).on("tap", ".close", function() {
		panelObj.clearAll();
		//payPanel.closePanel();//关闭弹窗
		panelObj.cancelFn!=undefined?panelObj.cancelFn():""; 
		document.getElementsByClassName("pays")[0].style.display = "none"; 
		this.isShow = false;
		
	});
})(window)

/*==================================================
输入支付密码  结束
 ==================================================*/